package org.darcy.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.darcy.config.property.AppProperties;
import org.darcy.dao.SysConfigMapper;
import org.darcy.entity.SysConfig;
import org.darcy.framework.blog.business.consts.DateConst;
import org.darcy.framework.blog.business.enums.ConfigKeyEnum;
import org.darcy.framework.blog.business.enums.FileUploadType;
import org.darcy.framework.blog.file.FileUploader;
import org.darcy.framework.blog.file.entity.VirtualFile;
import org.darcy.framework.blog.plugin.file.GlobalFileUploader;
import org.darcy.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 系统配置
 */
@Slf4j
@Service
public class SysConfigServiceImpl implements SysConfigService {

	@Autowired
	private SysConfigMapper sysConfigMapper;

	/**
	 * 获取系统配置
	 *
	 * @return
	 */
	@Override
	public Map<String, Object> getConfigs() {
		List<SysConfig> list = sysConfigMapper.selectList(null);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		String updateTimeKey = ConfigKeyEnum.UPDATE_TIME.getKey();
		Map<String, Object> res = new HashMap<>();
		res.put(updateTimeKey, DateUtil.parse("2019-01-01 00:00:00", DateConst.YYYY_MM_DD_HH_MM_SS_EN));
		list.forEach((sysConfig) -> {
			res.put(String.valueOf(sysConfig.getConfigKey()), sysConfig.getConfigValue());
			if (sysConfig.getUpdateTime().after(((Date) res.get(updateTimeKey)))) {
				res.put(updateTimeKey, sysConfig.getUpdateTime());
			}
		});
		String storageType = (String) res.get(ConfigKeyEnum.STORAGE_TYPE.getKey());
		if ("local".equalsIgnoreCase(storageType)) {
			res.put("fileStoragePath", null);
		} else if ("qiniu".equalsIgnoreCase(storageType)) {
			res.put("fileStoragePath", res.get(ConfigKeyEnum.QINIU_BASE_PATH.getKey()));
		} else if ("aliyun".equalsIgnoreCase(storageType)) {
			res.put("fileStoragePath", res.get(ConfigKeyEnum.ALIYUN_FILE_URL.getKey()));
		}
		return res;
	}

	@Override
	public void saveFile(String key, MultipartFile file) {
		if (key == null) {
			return;
		}
		if (file != null) {
			FileUploader uploader = new GlobalFileUploader();
			VirtualFile virtualFile = uploader.upload(file, FileUploadType.QRCODE.getPath(), true);
			this.saveConfig(key, virtualFile.getFullFilePath());
		}
	}

	@Override
	public void saveConfig(String key, String value) {
		if (!StringUtils.isEmpty(key)) {
			SysConfig config = null;
			if (null == (config = this.getByKey(key))) {
				config = new SysConfig();
				config.setConfigKey(key);
				config.setConfigValue(value);
				config.setCreateTime(new Date());
				config.setUpdateTime(new Date());
				this.sysConfigMapper.insert(config);
			} else {
				config.setConfigKey(key);
				config.setConfigValue(value);
				config.setUpdateTime(new Date());
				this.sysConfigMapper.updateById(config);
			}
		}
	}

	@Override
	public SysConfig getByKey(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		SysConfig sysConfig = new SysConfig();
		sysConfig.setConfigKey(key);
		QueryWrapper<SysConfig> q = new QueryWrapper<SysConfig>();
		q.eq("config_key", key);
		return this.sysConfigMapper.selectOne(q);
	}

	/**
	 * 添加/修改系统配置
	 *
	 * @param configs 所有的配置项
	 */
	@Override
	public void saveConfig(Map<String, String> configs) {
		if (!CollectionUtils.isEmpty(configs)) {
			configs.forEach(this::saveConfig);
		}
	}

	/**
	 * 获取网站详情
	 */
	@Override
	public Map<String, Object> getSiteInfo() {
		Map<String, Object> siteInfo = sysConfigMapper.getSiteInfo();
		if (!CollectionUtils.isEmpty(siteInfo)) {
			Date installdate = null;
			SysConfig config = this.getByKey(ConfigKeyEnum.INSTALLDATE.getKey());
			if (null == config || StringUtils.isEmpty(config.getConfigValue())) {
				// 默认建站日期为2019-01-01
				installdate = Date.from(LocalDate.of(2019, 1, 1).atStartOfDay(ZoneId.systemDefault()).toInstant());
			} else {
				installdate = DateUtil.parse(config.getConfigValue(), DatePattern.NORM_DATETIME_PATTERN);
			}
			long between = 1;
			if (!installdate.after(new Date())) {
				between = DateUtil.between(installdate, new Date(), DateUnit.DAY);
			}
			siteInfo.put("installdate", between < 1 ? 1 : between);
		}
		return siteInfo;
	}

	@Override
	public String getSpiderConfig() {
		SysConfig config = this.getByKey(ConfigKeyEnum.BLOG_HUNTER_CONFIG.getKey());
		if (config == null) {
			return "{}";
		}
		return StringUtils.isEmpty(config.getConfigValue()) ? "{}" : config.getConfigValue();
	}

	@Override
	public List<String> getRandomUserAvatar() {
		SysConfig config = this.getByKey("defaultUserAvatar");
		if (config == null) {
			return null;
		}
		try {
			return JSONArray.parseArray(config.getConfigValue(), String.class);
		} catch (Exception e) {
			log.error("配置项无效！defaultUserAvatar = [" + config.getConfigValue() + "]");
		}
		return null;
	}
}
